﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;

namespace CNCSoft.ErpApps.TMS.longACSS
{
    public partial class frmThongTinHocVien : Form
    {
        private SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        private string _invoicenumber;
        frmPhanTramChuyenCanHocVien frmChuyenCan = null;

        public frmThongTinHocVien()
        {
            InitializeComponent();
        }

        public frmThongTinHocVien(string invoicenumber)
        {
            InitializeComponent();
            _invoicenumber = invoicenumber;
        }

        private void frmThongTinHocVien_Load(object sender, EventArgs e)
        {
            try
            {
                LoadControl();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridView_KhoaHoc_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            try
            {
                //MessageBox.Show("OK");
                if (e.Column.Name == "gridColumn_PhanTramDiHoc")
                {
                    DataRow dr = gridView_KhoaHoc.GetDataRow(e.RowHandle);
                    frmChuyenCan = new frmPhanTramChuyenCanHocVien(dr["InvoiceNumber"].ToString(), dr["MaKhoaHoc"].ToString());
                    frmChuyenCan.ShowDialog();
                    frmChuyenCan.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region "Coding"
        private void LoadControl()
        {
            LoadData();
        }

        private void LoadData()
        {
            try
            {
                LoadEnquiryByInvoiceNumber(_invoicenumber);
                DataTable dt = ds.Tables["TableEnqByInvNum"];
                lbl_HoTen.Text = dt.Rows[0]["HoTen"].ToString();
                lbl_CMND.Text = dt.Rows[0]["SoCMND"].ToString();
                lbl_DiaChi.Text = dt.Rows[0]["DiaChi"].ToString();
                lbl_DienThoaiCoDinh.Text = dt.Rows[0]["DienthoaiCodinh"].ToString();
                lbl_DienThoaiDiDong.Text = dt.Rows[0]["DienthoaiDidong"].ToString();
                lbl_DienThoaiPhuHuynh.Text = dt.Rows[0]["DienthoaiPhuhuynh"].ToString();
                lbl_DonVi.Text = dt.Rows[0]["TenDonVi"].ToString();
                lbl_Email.Text = dt.Rows[0]["Email"].ToString();
                lbl_GioiTinh.Text = dt.Rows[0]["GioiTinh"].ToString();
                lbl_NgaySinh.Text = String.Format("{0:dd/MM/yyyy}", dt.Rows[0]["Ngaysinh"]);
                lbl_NgheNghiep.Text = dt.Rows[0]["TenNgheNghiep"].ToString();
                lbl_TenPhuHuynh.Text = dt.Rows[0]["HotenPhuhuynh"].ToString();
                pictureEdit_HinhAnh.EditValue = dt.Rows[0]["NoiDung"];
                //-----------------------Load Grid----------------------------------
                LoadInfoKhoaHocAndQTHTByEnquiryNumber(ds.Tables["TableEnqByInvNum"].Rows[0]["EnquiryNumber"].ToString());
                gridControl_KhoaHoc.DataSource = ds.Tables["TableInfoKHAndQTHT"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadEnquiryByInvoiceNumber(string invoicenumber)
        {
            try
            {
                if (ds.Tables["TableEnqByInvNum"] != null)
                    ds.Tables["TableEnqByInvNum"].Clear();
                string sql = @"select enq.EnquiryNumber, (Holot + ' ' + Ten) as HoTen, dmdv.TenDonVi, 
                                (case Gioitinh when 0 then N'Nữ' when 1 then N'Nam' end) as GioiTinh, Ngaysinh,
                                SoCMND, (DiachiThuongtru + ', ' + dmqh.TenQuanHuyen + ', ' + dmtp.TenTinhTP) as DiaChi,
                                DienthoaiCodinh, DienthoaiDidong, Email, dmnn.TenNgheNghiep, enq.HotenPhuhuynh, enq.DienthoaiPhuhuynh,
                                ha.NoiDung
                                from Enquiry enq 
                                left outer join DM_QUANHUYEN dmqh on dmqh.MaQuanHuyen = enq.QuanThuongTru
                                left outer join DM_TINHTP dmtp on dmtp.MaTinhTP = enq.ThanhPhoThuongtru
                                left outer join DM_NGHENGHIEP dmnn on dmnn.MaNgheNghiep = enq.NgheNghiep
                                left outer join DM_DONVI dmdv on dmdv.MaDonVi = enq.MaDonVi
                                left outer join Invoice inv on inv.EnquiryNumber = enq.EnquiryNumber
                                left outer join HINHANH ha on ha.EnquiryNumber = enq.EnquiryNumber
                                where inv.InvoiceNumber = '" + invoicenumber + @"'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableEnqByInvNum");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Load Info Khoa Hoc and QTHT of EnquiryNumber
        private void LoadInfoKhoaHocAndQTHTByEnquiryNumber(string enquirynumber)
        {
            try
            {
                if (ds.Tables["TableInfoKHAndQTHT"] != null)
                    ds.Tables["TableInfoKHAndQTHT"].Clear();
                string sql = @"usp_LoadInfoKhoaHocAndQTHTByEnquiryNumber @enquirynumber = '" + enquirynumber + @"'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableInfoKHAndQTHT");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
